// pages/goods/classify/classify.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: 'zeroBuy',
        classifySort: null,
        shelfId: 100,
        classifyId: null,
        classifyList: [],
        classifyGoods: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getClassifyList()
        this.getZeroBuyGoods()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    goSearchIndex: function() {
        wx.navigateTo({
            url: '/pages/search/searchIndex/searchIndex'
        })
    },

    getClassifyList: function() {
        const that = this
        let data = {
            activity: 785,
            type: 2
        }
        common.post('Shelf/Shelf', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                classifyList: res
            })
        }).catch(function(e) {})
    },

    getSortGoods: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let classifyGoods = this.data.classifyGoods
        let data = {
            activity: 785,
            order_by: this.data.classifySort,
            is_goods: 99,
            limit: 10,
            page: this.data.nowPage
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    classifyGoods: classifyGoods,
                    isAll: true
                })
                return
            }
            classifyGoods = classifyGoods.concat(res)
            that.setData({
                classifyGoods: classifyGoods,
                nowPage: that.data.nowPage + 1
            })
        }).catch(function(e) {})
    },

    getClassifyGoods: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let classifyGoods = this.data.classifyGoods
        let data = {
            activity: 785,
            is_goods: 99,
            class_id: this.data.classifyId,
            limit: 10,
            page: this.data.nowPage
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    classifyGoods: classifyGoods,
                    isAll: true
                })
                return
            }
            classifyGoods = classifyGoods.concat(res)
            that.setData({
                classifyGoods: classifyGoods,
                nowPage: that.data.nowPage + 1
            })
        }).catch(function(e) {})
    },

    getShelfGoods: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let classifyGoods = this.data.classifyGoods
        let data = {
            shelf_id: this.data.shelfId,
            limit: 10,
            page: this.data.nowPage
        }
        common.post('Shelf/Shelf_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    classifyGoods: classifyGoods,
                    isAll: true
                })
                return
            }
            classifyGoods = classifyGoods.concat(res)
            that.setData({
                classifyGoods: classifyGoods,
                nowPage: that.data.nowPage + 1
            })
        }).catch(function(e) {})
    },

    getZeroBuyGoods: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let classifyGoods = this.data.classifyGoods
        let data = {
            shelf_id: this.data.shelfId,
            limit: 10,
            page: this.data.nowPage
        }
        common.post('Shelf/Shelf_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    classifyGoods: classifyGoods,
                    isAll: true
                })
                return
            }
            classifyGoods = classifyGoods.concat(res)
            that.setData({
                classifyGoods: classifyGoods,
                nowPage: that.data.nowPage + 1
            })
        }).catch(function(e) {})
    },

    loadMoreGoods: function() {
        if (this.data.type == 'sort') {
            this.getSortGoods()
        } else if (this.data.type == 'class') {
            this.getClassifyGoods()
        } else if (this.data.type == 'shelf') {
            this.getShelfGoods()
        } else if (this.data.type == 'zeroBuy') {
            this.getZeroBuyGoods()
        }
    },

    classifyClick: function(e) {
        if (e.currentTarget.dataset.type == 'sort') {
            this.setData({
                type: 'sort',
                classifySort: e.currentTarget.dataset.sort,
                shelfId: null,
                classifyId: null,
                classifyGoods: [],
                nowPage: 0,
                isLoading: false,
                isAll: false
            })
            this.getSortGoods()
        } else if (e.currentTarget.dataset.type == 'class') {
            this.setData({
                type: 'class',
                classifySort: null,
                shelfId: null,
                classifyId: e.currentTarget.dataset.id,
                classifyGoods: [],
                nowPage: 0,
                isLoading: false,
                isAll: false
            })
            this.getClassifyGoods()
        } else if (e.currentTarget.dataset.type == 'shelf') {
            this.setData({
                type: 'shelf',
                classifySort: null,
                shelfId: e.currentTarget.dataset.id,
                classifyId: null,
                classifyGoods: [],
                nowPage: 0,
                isLoading: false,
                isAll: false
            })
            this.getShelfGoods()
        } else if (e.currentTarget.dataset.type == 'zeroBuy') {
            this.setData({
                type: 'zeroBuy',
                classifySort: null,
                shelfId: e.currentTarget.dataset.id,
                classifyId: null,
                classifyGoods: [],
                nowPage: 0,
                isLoading: false,
                isAll: false
            })
            this.getZeroBuyGoods()
        }
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id,
        })
    },

    goZeroBuyDetail: function(e) {
        wx.navigateTo({
            url: '/pages/zeroBuy/detail/detail?goodsId=' + e.currentTarget.dataset.id + '&shelfId=' + e.currentTarget.dataset.shelf
        })
    }
})